package com.zp.base.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.base.entity.bo.AdministrationDictBo;
import com.zp.base.entity.vo.AdministrationDictVo;
import com.zp.base.service.IAdministrationDictService;
import com.zp.common.core.constant.UserConstants;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.system.api.entity.SysBaseDictData;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 用药途径控制器
 * 前端访问路由地址为:/base/administrationDict
 *
 * @author zhang peng
 * @date 2023-01-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/administrationDict")
public class AdministrationDictController extends BaseController {

    private final IAdministrationDictService iAdministrationDictService;

    /**
     * 查询用药途径列表 (分页)
     */
    @SaCheckPermission("base:administrationDict:list")
    @GetMapping("/selectPage")
    public TableDataInfo<AdministrationDictVo> selectPage (AdministrationDictBo bo, PageQuery pageQuery) {
        return iAdministrationDictService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询用药途径列表 (不分页)
     */
    @SaCheckPermission("base:administrationDict:list")
    @GetMapping("/list")
    public R<List<AdministrationDictVo>> list (AdministrationDictBo bo) {
        List<AdministrationDictVo> list = iAdministrationDictService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出用药途径列表
     */
    @SaCheckPermission("base:administrationDict:export")
    @Log(title = "用药途径", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export (AdministrationDictBo bo, HttpServletResponse response) {
        List<AdministrationDictVo> list = iAdministrationDictService.queryList(bo);
        ExcelUtil.exportExcel(list, "用药途径", AdministrationDictVo.class, response);
    }

    /**
     * 获取用药途径详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("base:administrationDict:query")
    @GetMapping("/{id}")
    public R<AdministrationDictVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(iAdministrationDictService.queryById(id));
    }

    /**
     * 新增用药途径
     */
    @SaCheckPermission("base:administrationDict:add")
    @Log(title = "用药途径", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody AdministrationDictBo bo) {
        if (StringUtils.equals(UserConstants.NOT_UNIQUE, iAdministrationDictService.checkAdministrationDictUnique(bo))) {
            return R.fail("新增字典'" + bo.getLabel() + "'失败，字典值" + bo.getValue() + "已存在");
        }
        return toAjax(iAdministrationDictService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改用药途径
     */
    @SaCheckPermission("base:administrationDict:edit")
    @Log(title = "用药途径", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody AdministrationDictBo bo) {
        if (StringUtils.equals(UserConstants.NOT_UNIQUE, iAdministrationDictService.checkAdministrationDictUnique(bo))) {
            return R.fail("新增字典'" + bo.getLabel() + "'失败，字典值" + bo.getValue() + "已存在");
        }
        return toAjax(iAdministrationDictService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除用药途径
     *
     * @param ids 主键串
     */
    @SaCheckPermission("base:administrationDict:remove")
    @Log(title = "用药途径", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        return toAjax(iAdministrationDictService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    @SaCheckPermission("base:administrationDict:insert")
    @GetMapping("/listNotExistBaseData")
    public R<List<SysBaseDictData>> listNotExistBaseData (AdministrationDictBo bo) {
        List<SysBaseDictData> list = iAdministrationDictService.listNotExistBaseData(bo);
        return R.ok(list);
    }

    @PostMapping("/saveBatchAdministrationDictInData")
    @Log(title = "用药途径", businessType = BusinessType.SAVEBATCH)
    public R saveBatchAdministrationDictInData (@RequestBody AdministrationDictBo bo) {
        return R.ok(iAdministrationDictService.saveBatchAdministrationDictInData(bo));
    }
}
